package com.xue;

import com.alibaba.fastjson.JSONObject;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author xuejingbao
 * @create 2023-10-17 下午 15:52
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        List reLs1 = Arrays.asList(
                1,
                12,
                34,
                56,
                78,
                90
        );
        List reLs0 = Arrays.asList(
                0,
                12,
                34,
                56,
                78,
                90
        );
        byte[] msgByte1 = JSONObject.toJSONString(reLs1).getBytes(StandardCharsets.UTF_8);
        byte[] msgByte0 = JSONObject.toJSONString(reLs0).getBytes(StandardCharsets.UTF_8);
        AtomicBoolean steering = new AtomicBoolean(false);
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.execute(()->{
            Scanner scanner = new Scanner(System.in);
            while (true){
                if ("1".equals(scanner.next())) {
                    steering.set(!steering.get());
                    if (steering.get()){
                        System.out.println("切换成1模式");
                    }else{
                        System.out.println("切换成0模式");
                    }
                }
            }
        });
        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
        ses.scheduleAtFixedRate(() -> {
            for (int i = 0; i < 10; i++) {
                if (steering.get()){
                    GpsGetterHandler.sendAllChannel(msgByte1);
                }else{
                    GpsGetterHandler.sendAllChannel(msgByte0);
                }
            }
        }, 1, 1, TimeUnit.SECONDS);

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workGroup = new NioEventLoopGroup(2);
        ServerBootstrap bootstrap = new ServerBootstrap();
        GpsGetterHandler gpsGetterHandler = new GpsGetterHandler();
        bootstrap.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast("GPS工作线程", gpsGetterHandler);
                    }
                });
        ChannelFuture channelFuture = bootstrap.bind(1000).sync();
        System.out.println("TCP Server启动成功，端口是1000");
        Channel channel = channelFuture.channel();
        channel.closeFuture().addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                System.out.println("TCP Server已关闭，端口是1000");
            }
        });
    }

}
